﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Document
{
    /// <summary>
    /// 表示资源类型
    /// </summary>
    public enum ResourceType
    {
        /// <summary>
        /// 未知资源类型
        /// </summary>
        Unknown,

        /// <summary>
        /// 视频资源类型
        /// </summary>
        Video,

        /// <summary>
        /// 表示音频资源
        /// </summary>
        Audio,

        /// <summary>
        /// 表示图片资源
        /// </summary>
        Image,

        /// <summary>
        /// 表示着色器
        /// </summary>
        Shader,
    }

    /// <summary>
    /// 表示一个内容无关的资源键
    /// </summary>
    public class ResourceKey:IEqualityComparer<ResourceKey>,IEquatable<ResourceKey>
    {
        private ResourceType m_type;
        private Guid m_id;

        #region Constructor

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="type"></param>
        public ResourceKey(ResourceType type) : this(type, Guid.NewGuid()) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="guid"></param>
        internal ResourceKey(ResourceType type,Guid guid)
        {
            m_id = guid;
            m_type = type;
        }

        #endregion

        #region Property

        /// <summary>
        /// 获取当前资源的类型
        /// </summary>
        public ResourceType Type { get { return m_type; } }

        /// <summary>
        /// 获取当前资源的唯一标识
        /// </summary>
        public string Id { get { return m_id.ToString(); } }

        #endregion

        #region Override

        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool Equals(ResourceKey x, ResourceKey y)
        {
            return x.m_id == y.m_id && x.m_type == y.m_type;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int GetHashCode(ResourceKey obj)
        {
            return obj.m_type.GetHashCode() ^ obj.m_id.GetHashCode();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if(obj is ResourceKey key) {
                return key.m_id == this.m_id && key.m_type == this.m_type;
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.m_type.GetHashCode() ^ this.m_id.GetHashCode();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static bool operator !=(ResourceKey x,ResourceKey y)
        {
            return x.m_id != y.m_id || x.m_type != y.m_type;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static bool operator ==(ResourceKey x,ResourceKey y)
        {
            return x.m_id == y.m_id && x.m_type == y.m_type;
        }

        /// <summary>
        /// 获取与当前对象等价的字符串形式
        /// </summary>
        /// <returns>返回与当前对象等价的字符串形式</returns>
        public override string ToString()
        {
            return m_id.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(ResourceKey other)
        {
            return this.m_id == other.m_id && this.m_type == other.m_type;
        }

        #endregion
    }
}
